from codec.Media import Media
from tkinter import BooleanVar, ttk, Tk, StringVar
from detect.color import detect
from tkinter.filedialog import askopenfilename, askdirectory
from threading import Thread


class Gui:
    def setupMainframe(self):
        self.mainframe: ttk.Frame = ttk.Frame(self.root)
        self.mainframe.grid(column=0, row=0)

        ttk.Label(self.mainframe, text='Filepath').grid(column=0, row=0)
        ttk.Entry(self.mainframe, textvariable=self.filepath).grid(
            column=1, row=0)
        ttk.Button(self.mainframe, text='Select', command=self.getFilename(self.filepath)).grid(
            column=2, row=0)

        ttk.Label(self.mainframe, text='Export to').grid(column=0, row=1)
        ttk.Entry(self.mainframe, textvariable=self.exportPath).grid(
            column=1, row=1)
        ttk.Button(self.mainframe, text='Select', command=self.getFolderName(self.exportPath)).grid(
            column=2, row=1)

    def setupSubmitFrame(self):
        self.submit_frame: ttk.Frame = ttk.Frame(self.root)
        self.submit_frame.grid(column=0, row=2)

        ttk.Checkbutton(self.submit_frame, variable=self.inOne,
                        text='Export in one').grid(row=0)

        ttk.Button(self.submit_frame, text='export',
                   command=self.export).grid(row=1)

    def __init__(self, root: Tk):
        self.root: Tk = root
        self.filepath: StringVar = StringVar()
        self.exportPath: StringVar = StringVar()
        self.inOne: BooleanVar = BooleanVar()

        self.setupMainframe()
        self.setupSubmitFrame()

    def getFilename(self, pathVar: StringVar):
        return lambda: pathVar.set(askopenfilename(title='Select video..', filetypes=[
            ('video', '.flv'),
            ('video', '.mp4')
        ]))

    def getFolderName(self, pathVar: StringVar):
        return lambda: pathVar.set(askdirectory(title='Select directory..'))

    def export(self):
        media: Media = Media(self.filepath.get(), detect(self.filepath.get()))
        media.export(self.exportPath.get(), inOne=self.inOne.get())


def main():
    root: Tk = Tk()
    Gui(root)
    root.mainloop()
